/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.PluginDetail;
import me.zhengjie.modules.system.domain.vo.PluginDetailAuditVo;
import me.zhengjie.modules.system.domain.vo.PluginDetailQueryCriteria;
import me.zhengjie.modules.system.service.PluginDetailService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author lxp
* @date 2024-11-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "PluginDetailController管理")
@RequestMapping("/api/pluginDetail")
public class PluginDetailController {

    private final PluginDetailService pluginDetailService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('pluginDetail:list')")
    public void exportPluginDetail(HttpServletResponse response, PluginDetailQueryCriteria criteria) throws IOException {
        pluginDetailService.download(pluginDetailService.queryAll(criteria), response);
    }

    @GetMapping("/listByParent/{parentId}")
    @Log("根据插件id查询插件版本列表")
    @ApiOperation("根据插件id查询插件版本列表")
    //@PreAuthorize("@el.check('pluginDetail:list')")
    public ResponseEntity<List<PluginDetail>> listByParentId(@PathVariable Long parentId, @RequestParam(required = false) String status){
        return new ResponseEntity<>(pluginDetailService.queryByParentId(parentId,status),HttpStatus.OK);
    }

    @GetMapping("/{id}")
    @Log("插件版本详情")
    @ApiOperation("插件版本详情")
    //@PreAuthorize("@el.check('pluginDetail:list')")
    public ResponseEntity<PluginDetail> queryById(@PathVariable Long id){
        return new ResponseEntity<>(pluginDetailService.getPluginDetailById(id),HttpStatus.OK);
    }

    @PostMapping("/audit")
    @Log("插件版本审批")
    @ApiOperation("插件版本审批")
    //@PreAuthorize("@el.check('pluginDetail:audit')")
    public ResponseEntity<Boolean> audit(@Validated @RequestBody PluginDetailAuditVo audit){
        return new ResponseEntity<>(pluginDetailService.audit(audit),HttpStatus.OK);
    }
}